package com.adrian.misc.fractal;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.Vector;

import javax.swing.JComponent;
import javax.swing.event.ChangeEvent;

import com.adrian.misc.fractal.color.ColorMap;

public class ColorControl extends JComponent implements MouseListener {

	Vector<ColorMap> colorMaps = new Vector<ColorMap>();
	int widgetHeight = 0; 	
	int selectedMap = 0;
	BufferedImage backBuffer = null;
	Graphics bg = null;
	FractalCanvas fCanvas ;
	ColorMapListner colorMapListner;
	
	public ColorControl (FractalCanvas fCan, int width, int height){ 
		super();
		fCanvas = fCan;
		System.out.println("Setting size- "+ width+ "," + height);
		this.setPreferredSize(new Dimension(width,height));
		System.out.println("size- "+ this.getPreferredSize().width+ "," + this.getPreferredSize().height);
		this.setOpaque(true);
		this.setBackground(Color.WHITE);
		addMouseListener(this);

	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public void add (ColorMap colMap) {
		colorMaps.add(colMap);
		repaint();
	}
	
	@Override
	public void paintComponent(final Graphics g) {
		if (backBuffer == null){
			//getWidth instead of getpreferred width as it is causing artifacts with scrollpane
			backBuffer = new BufferedImage(getWidth(),getPreferredSize().height,BufferedImage.TYPE_INT_RGB);
			bg = backBuffer.getGraphics();
			
		}
		bg.setColor(Color.BLACK);
		bg.fillRect(0, 0, getWidth(), getHeight());
		System.out.println("Add called - " + colorMaps.size());
		if ( colorMaps.size() >0) { 
			widgetHeight = (getPreferredSize().height/colorMaps.size() > 50) ? 50:(getPreferredSize().height/colorMaps.size());
			for (int i = 0;i<colorMaps.size(); i++) {
				//using getWidth instead of preferred width
				Image im = new BufferedImage(getWidth()-4, widgetHeight, BufferedImage.TYPE_INT_RGB);
				colorMaps.get(i).demo(im, this);
				bg.drawImage(im, 2, 2 + i*widgetHeight,this);
			}
		}
		System.out.println("Color control paint called---" + backBuffer.getWidth()+ " pref="+ getPreferredSize().width +" Actul=" + getWidth());
		g.drawImage(backBuffer,0,0,this);
		g.setColor(Color.WHITE);
		g.drawRect(1, 1+ selectedMap*widgetHeight, getWidth()-3 , widgetHeight);
	}
	
	public void mouseClicked(MouseEvent e) {
		int index = e.getY()/widgetHeight;
		if (index > colorMaps.size()) {
			return;
		} 
		selectedMap = index;
		repaint();
		colorMapListner.colorControlHasChanged(new ChangeEvent(this));
		fCanvas.setColorSync(false);
		fCanvas.repaint();
	}
	public void fireSelection() {
		colorMapListner.colorControlHasChanged(new ChangeEvent(this));
	}
	public ColorMap getSelectedColorMap () {
		return colorMaps.get(selectedMap);
	}
	public void registerListner (ColorMapListner cml) {
		colorMapListner = cml;
	}
	public void mouseEntered(MouseEvent arg0) {}
	public void mouseExited(MouseEvent arg0) {}
	public void mousePressed(MouseEvent arg0) {}
	public void mouseReleased(MouseEvent arg0) {}

}
